package com.gogowan.agroenterprise.newview;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.gogowan.agroenterprise.MyApplication;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.contorl.RetrofitUtil.GsonUtil;
import com.gogowan.agroenterprise.model.BoxCode;
import com.gogowan.agroenterprise.model.DealerInStorageRequest;
import com.gogowan.agroenterprise.model.DealerUnConfirmDetailRequest;
import com.gogowan.agroenterprise.model.DealerUnConfirmRequest;
import com.gogowan.agroenterprise.model.DealerUnConfirmResult;
import com.gogowan.agroenterprise.model.IntStorageBoxResult;
import com.gogowan.agroenterprise.model.OutStore;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.model.StoreListResult;
import com.gogowan.agroenterprise.model.StoreModel;
import com.gogowan.agroenterprise.model.StoreRequest;
import com.gogowan.agroenterprise.model.UserType;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.Base_Adapater;

import java.util.ArrayList;
import java.util.List;

public class NewInStorageActivityTowInforMation extends BaseListView {
    private ProgressDialog progressDialog;
    private List<BoxCode> BoxInfo=new ArrayList<>();
    private List<StoreModel> StoreList=new ArrayList<>();
    private Base_Adapater adapter;
    private OutStore outStore;
    private Base_Adapater StorageAdapater;
    private Spinner StorageListView;
    private StoreModel storeModel;

    @Override
    protected void InitValue() {

    }

    @Override
    protected void InitView() {
        outStore=(OutStore)getIntent().getSerializableExtra("bean");
        SetHeadView(true,R.layout.activity_newinstorage_head);
        SetFootView(true,R.layout.layout_one_button);
        StorageListView=(Spinner) getHeadView().findViewById(R.id.new_in_storage_spinner);
        StoreModel initstoreModel=new StoreModel();
        initstoreModel.setStoreName("请选择仓库");
        StoreList.add(initstoreModel);
        adapter=new Base_Adapater(this,BoxInfo);
        StorageAdapater=new Base_Adapater(this,StoreList);
        StorageAdapater.setOnItemListener(new Base_Adapater.OnItemListener() {
            @Override
            public String ItemStr(int position) {
                return StoreList.get(position).getStoreName();
            }
        });
        adapter.setOnItemListener(new Base_Adapater.OnItemListener() {
            @Override
            public String ItemStr(int position) {
                return "产品名称: "+BoxInfo.get(position).getMaterialName()
                        +"\n装箱日期: "+BoxInfo.get(position).getPackDate()
                        +"\n产品数量: "+BoxInfo.get(position).getMaCount()+"个"
                        +"\n垛   数: "+BoxInfo.get(position).getDuoCount()+"垛"
                        +"\n箱   数: "+BoxInfo.get(position).getBoxCount()+"箱"
                        +"\n礼 盒 数: "+BoxInfo.get(position).getGiftCount()+"盒";
            }
        });
        StorageListView.setAdapter(StorageAdapater);
        getListView().setAdapter(adapter);
        StorageListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (!StoreList.get(position).getStoreName().equals("请选择仓库")){
                    storeModel=StoreList.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        getFootView().findViewById(R.id.in_storage_qrrk_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SaveHttp();
            }
        });
        QueryStorageHttp();
    }

    private void QueryStorageHttp() {
        progressDialog = ProgressDialog.show(this, getString(R.string.alert_title), getString(R.string.alert_message),
                true, false);

        StoreRequest storeRequest = new StoreRequest();
        //storeRequest.setEnterpriseID(getLogInfo().getEnterpriseinfo().getEnterpriseid());
        //storeRequest.setDealerID(getLogInfo().getEnterpriseinfo().getDealerId());
        //storeRequest.setUserType(getLogInfo().getEnterpriseinfo().getUserType());
        //storeRequest.setUserID(getLogInfo().getUserinfo().getPersonid());
        storeRequest.setPageIndex(1);
        storeRequest.setType(0);
        storeRequest.setParentCode(0);
        Http(storeRequest, "21000", progressDialog, new RetrofitHttpListener() {
            @Override
            public void OnJson(String str) {
                StoreListResult storeListResult = (StoreListResult) Utils.fromJSON(str, StoreListResult.class);
                if (storeListResult.getResult()>0){
                    StoreList.addAll(storeListResult.getStoreList());
                    StorageAdapater.notifyDataSetChanged();
                }
            }
        });
        QueryHttp();
    }

    private void QueryHttp() {
        DealerUnConfirmDetailRequest request=new DealerUnConfirmDetailRequest();
        progressDialog = ProgressDialog.show(this,
                getString(R.string.alert_title),
                getString(R.string.alert_message), true, false);
        request.setOutStorageID(outStore.getOutStorageID());
        Http(request, "18001", progressDialog,new RetrofitHttpListener() {
            @Override
            public void OnJson(String str) {
                IntStorageBoxResult result= GsonUtil.GsonToBean(str,IntStorageBoxResult .class);
                showToast(result.getResultinfo());
                if (result.getResult()>0){
                    BoxInfo.addAll(result.getBoxInfo());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }
    private void SaveHttp(){
        DealerInStorageRequest request=new DealerInStorageRequest();
        request.setOutStorageID(outStore.getOutStorageID());
        if(storeModel!=null){
            request.setStorageId(storeModel.getStoreCode());
        }
        //request.setUserId(getLogInfo().getUserinfo().getPersonid());
        request.setOrder(outStore.getOutStorageNO());
        if (MyApplication.mSettings.getInt("usertype",0)== UserType._JING_XIAO_SHANG.getTypeID()){
           // request.setFirstDealerId(getLogInfo().getEnterpriseinfo().getDealerId());
        }else {
           // request.setDealerId(getLogInfo().getEnterpriseinfo().getDealerId());
        }
        progressDialog = ProgressDialog.show(this,
                getString(R.string.alert_title),
                getString(R.string.alert_message), true, false);
        Http(request, "182001", progressDialog, new RetrofitHttpListener() {
            @Override
            public void OnJson(String str) {
                ResultInfo resultInfo=GsonUtil.GsonToBean(str,ResultInfo.class);
                showToast(resultInfo.getResultinfo());
                if (resultInfo.getResult()>0){
                    finish();
                }
            }
        });

    }
}
